package com.me.study.javaBasic.collection;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.util.Map;

/**
 * 测试 guava table（Map<R, Map<C,V>>），值也是 Map 的 Map
 *
 * @author bin.yin
 */
public class TestTable {

    private static Table<String, String, String> table = HashBasedTable.create();

    public static void main(String[] args) {

        // row+column对应的value
        System.out.println("\nget row column：");
        System.out.println(table.get("101", "IBM"));

        // 所有员工名称
        System.out.println("\nvalues：");
        System.out.println(table.values());

        // 所有单元格数据
        System.out.println("\ncellSet：");
        System.out.println(table.cellSet());

        //IBM公司中所有信息
        System.out.println("\nrow：");
        Map<String, String> ibmEmployees = table.row("103");
        for (Map.Entry<String, String> entry : ibmEmployees.entrySet()) {
            System.out.println("company: " + entry.getKey() + ", empName: " + entry.getValue());
        }

        // 所有id
        System.out.println("\nrowKeySet：相当于 id");
        System.out.println(table.rowKeySet());

        //公司中的所有员工和员工编号
        System.out.println("\nrowMap：");
        System.out.println(table.rowMap());

        //得到员工编号为102的所有公司和姓名
        System.out.println("\ncolumn：");
        Map<String, String> employerMap = table.column("Microsoft");
        for (Map.Entry<String, String> entry : employerMap.entrySet()) {
            System.out.println("empId: " + entry.getKey() + ", empName: " + entry.getValue());
        }

        // 所有列名
        System.out.println("\ncolumnKeySet：排重后的 列");
        System.out.println(table.columnKeySet());

        //员工编号对应的公司和员工名称
        System.out.println("\ncolumnMap");
        System.out.println(table.columnMap());
    }

    static {
        table.put("101", "IBM", "Mahesh");
        table.put("102", "IBM", "Ramesh");
        table.put("103", "IBM", "Suresh");

        table.put("111", "Microsoft", "Sohan");
        table.put("112", "Microsoft", "Mohan");
        table.put("113", "Microsoft", "Rohan");

        table.put("121", "TCS", "Ram");
        table.put("102", "TCS", "Shyam");
        table.put("123", "TCS", "Sunil");
    }

}
